package liangtWorkSpace.list;

/**
 * @ClassName ListPAll
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/18 16:02
 */

/**
 * 链表环-判断是否是链表环
 */
public class ListPAll {

    /**
     * 利用追击法判断是否是链表环
     * @param node
     * @return
     */
    public static boolean isCycle(Node node){
        Node p1 = node;
        Node p2 = node;
        while (p2 != null && p2.next != null ){
            p1 = p1.next;
            p2 =p2.next.next;
            if (p1 == p2){
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        Node node1 = new Node(5);
        Node node2 = new Node(3);
        Node node3 = new Node(7);
        Node node4 = new Node(2);
        Node node5 = new Node(6);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node2;
        System.out.println(isCycle(node1));

    }
}
class Node{
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
    }
}
